package com.controller;

import com.alibaba.fastjson.JSONObject;


import com.entity.User;
import com.github.pagehelper.Page;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController // 定义为控制器 返回JSON类型数据
@RequestMapping(value = "/equipment/user", produces = "application/json; charset=utf-8") // 设置请求路径
@CrossOrigin // 允许跨域访问其资源
public class UserController extends BaseController{
    @Autowired // @Autowired的作用是自动注入依赖的ServiceBean
    private UserService userService;


//添加用户
    @PostMapping(value = "insertUser.action")
    @ResponseBody // 将java对象转为json格式的数据返回
    public Map<String, Object> insertUser(@RequestBody String jsonStr) {
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject obj = JSONObject.parseObject(jsonStr); // 将JSON字符串转换成object
        User user = new User();
        user.setUsername(obj.getString("username")); // 为用户名赋值
        user.setPassword(obj.getString("password")); // 为密码赋值
        user.setRealname(obj.getString("realname")); // 为姓名赋值
        user.setSex(obj.getString("sex")); //
        user.setAge(obj.getString("age")); //
        user.setTel(obj.getString("tel")); //
        user.setType(obj.getString("type"));
        int num = this.userService.insertUser(user);
        if (num > 0) {
            map.put("success", true);
            map.put("code", num);
            map.put("message", "保存成功");
        } else {
            map.put("success", false);
            map.put("code", num);
            map.put("message", "保存失败");
        }
        return map;
    }
    @GetMapping(value = "getUserByPage.action")
    @ResponseBody // 将java对象转为json格式的数据返回
    public Map<String, Object> getUserByPage(@RequestParam(defaultValue = "1") Integer page,
                                              @RequestParam(defaultValue = "10") Integer limit) {
        // 定义一个Map对象 用来返回数据
        Map<String, Object> map = new HashMap<String, Object>();
        Page<User> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
        List<User> list = this.userService.getAllUser();
        // 返回的map中定义数据格式
        map.put("count", pager.getTotal());
        map.put("total", list.size());
        map.put("data", list);
        map.put("code", 0);
        map.put("msg", "");
        map.put("page", page);
        map.put("limit", limit);
        return map;
    }
    // 按主键删除一个管理员
    @GetMapping(value = "deleteUser.action")
    @ResponseBody // 将java对象转为json格式的数据返回
    public Map<String, Object> deleteUser(String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        int num = this.userService.deleteUser(id);
        if (num > 0) {
            map.put("success", true);
            map.put("code", num);
            map.put("message", "删除成功");
        } else {
            map.put("success", false);
            map.put("code", num);
            map.put("message", "删除失败");
        }
        return map;
    }

    // 按主键批量删除管理员
    @PostMapping(value = "deleteUserByIds.action")
    @ResponseBody // 将java对象转为json格式的数据返回
    public Map<String, Object> deleteUserByIds(@RequestBody String[] ids) {
        int num = 0;
        for (String id : ids) {
            num += this.userService.deleteUser(id);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        if (num > 0) {
            map.put("success", true);
            map.put("code", num);
            map.put("message", "删除成功");
        } else {
            map.put("success", false);
            map.put("code", num);
            map.put("message", "删除失败");
        }
        return map;
    }

    // 修改
    @PostMapping(value = "updateUser.action")
    @ResponseBody // 将java对象转为json格式的数据返回
    public Map<String, Object> updateUser(@RequestBody String jsonStr) {
        JSONObject obj = JSONObject.parseObject(jsonStr); // 将JSON字符串转换成object
        User user = this.userService.getUserById(obj.getString("id")); // 获取object中id字段
        user.setId(obj.getString("id"));
        user.setUsername(obj.getString("username")); // 为用户名赋值
        user.setPassword(obj.getString("password"));
        user.setRealname(obj.getString("realname")); // 为姓名赋值
        user.setTel(obj.getString("tel")); // 为联系方式赋值
        user.setAge(obj.getString("age"));
        user.setSex(obj.getString("sex"));
        user.setType(obj.getString("type"));
        Map<String, Object> map = new HashMap<String, Object>();
        int num = this.userService.updateUser(user);
        if (num > 0) {
            map.put("success", true);
            map.put("code", num);
            map.put("message", "修改成功");
        } else {
            map.put("success", false);
            map.put("code", num);
            map.put("message", "修改失败");
        }
        return map;
    }
    @GetMapping(value = "getUserById.action")
    @ResponseBody // 将java对象转为json格式的数据返回
    public User getUserById(String id) {
        User user = this.userService.getUserById(id);
        return user;
    }
    @PostMapping("edituserpwd.action") // 定义访问方法路径
    @ResponseBody // 将java对象转为json格式的数据
    public Map<String, Object> edituserpwd(@RequestBody String jsonStr) {
        JSONObject obj = JSONObject.parseObject(jsonStr); // 将传递的Json参数 转换成对象类型
        String id = obj.getString("id"); // 主键
        String password = obj.getString("password"); // 原密码
        String repassword = obj.getString("repassword"); // 新密码
        Map<String, Object> map = new HashMap<String, Object>(); // 定义Map 其为返回值
       User user = this.userService.getUserById(id); //
        if (password.equals(user.getPassword())) { // 校验原密码是否正确
           user.setPassword(repassword); // 重置密码
            this.userService.updateUser(user); // 更新数据
            map.put("success", true);
            map.put("message", "修改成功");
        } else {
            map.put("success", false);
            map.put("message", "旧密码错误");
        }
        return map;
    }
    @GetMapping(value = "getUserByUsername.action")
    @ResponseBody // 将java对象转为json格式的数据返回
    public Map<String, Object> getUserByUsername(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit,String username) {
        // 定义一个Map对象 用来返回数据
        Map<String, Object> map = new HashMap<String, Object>();
        Page<User> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
        List<User> list = this.userService.getUserByUsername(username);
        // 返回的map中定义数据格式
        map.put("count", pager.getTotal());
        map.put("total", list.size());
        map.put("data", list);
        map.put("code", 0);
        map.put("msg", "");
        map.put("page", page);
        map.put("limit", limit);
        return map;
    }
}
